package com.abing.starry.strategy.api;

import com.abing.api.model.StarryApiRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Author CaptainBing
 * @Date 2024/8/24 22:12
 * @Description api调用执行策略
 */
@Component
@RequiredArgsConstructor
public class InvokeStrategyExecutor {

    private final DefaultInvokeStrategy defaultInvokeStrategy;

    private final StarryInvokeStrategy starryInvokeStrategy;


    public String doInvoke(StarryApiRequest starryApiRequest, boolean isStarryApi) {

        return isStarryApi
                ? starryInvokeStrategy.doInvoke(starryApiRequest)
                : defaultInvokeStrategy.doInvoke(starryApiRequest);

    }



    private final List<InvokeStrategy> invokeStrategyList;

    /**
     * 复杂策略即可采用这种注解执行策略
     * @param starryApiRequest
     * @param targetInvokeStrategy
     * @return
     */
    public String doInvoke(StarryApiRequest starryApiRequest, InvokeStrategyEnum targetInvokeStrategy) {
        for (InvokeStrategy invokeStrategy : invokeStrategyList) {
            if (invokeStrategy.getClass().isAnnotationPresent(InvokeStrategyConfig.class)) {
                InvokeStrategyConfig invokeStrategyConfig = invokeStrategy.getClass().getAnnotation(InvokeStrategyConfig.class);
                InvokeStrategyEnum invokeStrategyEnum = invokeStrategyConfig.strategy();
                if (targetInvokeStrategy.equals(invokeStrategyEnum)) {
                    invokeStrategy.doInvoke(starryApiRequest);
                }
            }
        }
        return null;
    }

}
